/*
 * Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
 */
function confirmDelete(deptId) {
	showConfirmPopup("Confirm", "Are you sure to delete the selected department?",
		function () {
			deleteDept(deptId);
		}
	);
}

function deleteDept(id){
	$.ajax({
		url: "delete_department",
		data: {
			id: id
		},
		type: "post",
		dataType: "json",
		success: function (data) {
			if (data.status == "ok") {
				location.reload();
			} else if (data.errorCode == -2) {
				showErrorPopup("Error", "Insufficient Privilege");
			} else if (data.errorCode == -3) {
				showConfirmPopup("Warning", "There are users in this department. Do you wish to delete them?",
					function () {deleteUserInDept(id,function () {deleteDept(id);});},null);
			}
		},

		error: function (jqHXR, textStatus, error) {
			console.log(error);
		}
	});
}

function deleteUserInDept(id, successCallback) {
	$.ajax({
		url: "delete_users_in_dept",
		data: {
			id: id
		},
		type: "post",
		dataType: "json",
		success: function (data) {
			if (data.status == "ok") {
				successCallback();
			} else{
				showErrorPopup("Error", data.errorMessage);
			}
		},

		error: function (jqHXR, textStatus, error) {
			console.log(error);
		}
	});
}